home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / ctlib100.zip / INSTALL.LZH / ARRAYS9.PAS < prev    next >
Pascal/Delphi Source File  |  1996-10-12  |  4KB  |  136 lines

  1. {**************************************************************************}
  2. {*  BitSoft Development, L.L.C.                                           *}
  3. {*  Copyright (C) 1995, 1996 BitSoft Development, L.L.C.                  *}
  4. {*  All rights reserved.                                                  *}
  5. {*  Containers Library demo                                               *}
  6. {**************************************************************************}
  7.  
  8. program Arrays9;
  9.  
  10. {$X+}
  11.  
  12. { Sample program for using a stream object array }
  13.  
  14. uses Objects, Containr, ctArrays,
  15.      {$ifdef Windows}
  16.      WinCtr;
  17.      {$else}
  18.      Crt;
  19.      {$endif}
  20.  
  21. type
  22.   String20 = string[20];
  23.  
  24. type
  25.   PWeatherInfo = ^TWeatherInfo;
  26.   TWeatherInfo = object(TObject)
  27.       Location : PString;
  28.       Humidity : Integer;
  29.       Rain : Integer;
  30.     constructor Init(ALocation : String20; AHumidity, ARain : Integer);
  31.     constructor Load(var S: TStream);
  32.     procedure Store(var S: TStream);
  33.     destructor Done; virtual;
  34.   end; { TWeatherInfo }
  35.  
  36. constructor TWeatherInfo.Init(ALocation : String20; AHumidity,
  37.   ARain : Integer);
  38. begin
  39.   Location := NewStr(ALocation);
  40.   Humidity := AHumidity;
  41.   Rain := ARain;
  42. end;
  43.  
  44. constructor TWeatherInfo.Load(var S: TStream);
  45. begin
  46.   Location := S.ReadStr;
  47.   S.Read(Humidity, SizeOf(Humidity));
  48.   S.Read(Rain, SizeOf(Rain));
  49. end;
  50.  
  51. procedure TWeatherInfo.Store(var S: TStream);
  52. begin
  53.   S.WriteStr(Location);
  54.   S.Write(Humidity, SizeOf(Humidity));
  55.   S.Write(Rain, SizeOf(Rain));
  56. end;
  57.  
  58. destructor TWeatherInfo.Done;
  59. begin
  60.   DisposeStr(Location);
  61. end;
  62.  
  63. const
  64.   RWeatherInfo : TStreamRec = (
  65.     ObjType: 1000;
  66.     VmtLink: Ofs(TypeOf(TWeatherInfo)^);
  67.     Load:    @TWeatherInfo.Load;
  68.     Store:   @TWeatherInfo.Store);
  69.  
  70. procedure DisplayWeatherData(WeatherData : PSequence);
  71. var
  72.   i : Integer;
  73.   Item : Pointer;
  74. begin
  75.   with WeatherData^ do
  76.     for i := FirstIndex to LastIndex do
  77.     begin
  78.       Item := At(i);
  79.       with PWeatherInfo(Item)^ do
  80.         writeln('Hour: ', i:2, ':00', '':3, Location^, '':20 -
  81.           Length(Location^), Humidity, '':5, Rain:5);
  82.       DoneItem(Item); { required }
  83.     end; { for }
  84. end;
  85.  
  86. procedure FindLowHumidityValue(WeatherData : PSequence);
  87. var
  88.   Item : Pointer;
  89.   Index : LongInt;
  90.  
  91.   function HasLowHumidity(Item : PWeatherInfo) : Boolean; far;
  92.   begin
  93.     HasLowHumidity := (Item^.Humidity < 22);
  94.   end;
  95.  
  96. begin
  97.   Item := WeatherData^.FirstThat(@HasLowHumidity, Index);
  98.   writeln ('First with low humidity (H < 22):');
  99.   with PWeatherInfo(Item)^ do
  100.     writeln('Hour: ', Index:2, ':00', '':3, Location^, '':20 -
  101.       Length(Location^), Humidity, '':5, Rain:5);
  102.   WeatherData^.DoneItem(Item);
  103. end;
  104.  
  105. var
  106.   MorningWeatherData : PEmsObjectArray;
  107.  
  108. begin
  109.   ClrScr;
  110.  
  111.   { Create the array: data size must be equal to the maximum size of
  112.     the data that will be stored in the array. }
  113.   MorningWeatherData := New(PEmsObjectArray, Init(7, 11,
  114.     SizeOf(String20) + (2 * SizeOf(Integer))));
  115.  
  116.   { Register the TWeatherInfo object }
  117.   RegisterType(RWeatherInfo);
  118.  
  119.   { Insert the items in the array }
  120.   with MorningWeatherData^ do
  121.   begin
  122.     AtInsert(7, New(PWeatherInfo, Init('Miami', 34, 0)));
  123.     AtInsert(8, New(PWeatherInfo, Init('Helsinski', 23, 3)));
  124.     AtInsert(9, New(PWeatherInfo, Init('Canada', 26, 2)));
  125.     AtInsert(10, New(PWeatherInfo, Init('Berlin', 28, 5)));
  126.     AtInsert(11, New(PWeatherInfo, Init('Melbourne', 20, 0)));
  127.   end; { with }
  128.  
  129.   DisplayWeatherData(MorningWeatherData);
  130.   writeln;
  131.   FindLowHumidityValue(MorningWeatherData);
  132.  
  133.   { Dispose of the array }
  134.   Dispose(MorningWeatherData, Done);
  135. end.
  136.